package com.cloudweb.oa.service.impl;

import cn.js.fan.util.StrUtil;
import com.cloudweb.oa.service.FormService;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.flow.FormField;
import com.redmoon.oa.flow.FormParser;
import org.springframework.stereotype.Service;

import java.util.Vector;

@Service
public class FormServiceImpl implements FormService {

    @Override
    public String checkFormContent(String code, String content) {
        FormParser formParser = new FormParser(content);
        formParser.validateFields();
        Vector<FormField> newv = formParser.getFields();
        FormDb fd = new FormDb();
        fd = fd.getFormDb(code);
        // Vector<FormField> oldv = fd.getTableColumnsFromDb();
        Vector<FormField> oldv = fd.getFields();
        Vector<FormField>[] vt = FormDb.checkFieldChange(oldv, newv, fd.getFields());
        Vector<FormField> delv = vt[0];
        Vector<FormField> addv = vt[1];
        Vector<FormField> changev = vt[3];

        if (addv.size() == 0 && delv.size() == 0) {
            // out.print("<span style='color:green'>未增加或删除字段</span>");
        }

        String str = "";
        StringBuffer sbAdded = new StringBuffer();
        if (addv.size() > 0) {
            for (FormField ff : addv) {
                StrUtil.concat(sbAdded, "，", ff.getTitle());
            }
            str = "<span style='color:green'>增加字段：" + sbAdded.toString() + "</span><br/>";
        }

        StringBuffer sbDeleted = new StringBuffer();
        if (delv.size() > 0) {
            for (FormField ff : delv) {
                StrUtil.concat(sbDeleted, "，", ff.getTitle());
            }
            str += "<span style='color:red'>删除字段：" + sbDeleted.toString() + "</span>";
        }

        StringBuffer sbChanged = new StringBuffer();
        if (changev.size() > 0) {
            for (FormField ff : changev) {
                StrUtil.concat(sbChanged, "，", ff.getTitle() + "(" + ff.getFieldTypeDesc() + ")");
            }
            str += "<span style='color:blue'>更改类型：" + sbChanged.toString() + "</span>";
        }
        return str;
    }
}
